//
//  AddChargeViewController.m
//  CS Inventory Manager
//
//  Created by Billy Zoellers on 4/12/13.
//  Copyright (c) 2013 Transylvania University. All rights reserved.
//

#import "AddChargeViewController.h"
#import "ChargeHistoryViewController.h"
#import "Patron.h"

@interface AddChargeViewController ()

@end

@implementation AddChargeViewController
@synthesize delegate,inChargeAmt,inChargeType,inNotes;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    // allocate the segmented control
    inChargeType = [[UISegmentedControl alloc] initWithFrame:CGRectMake(100, 10, 185, 30)];
    [inChargeType insertSegmentWithTitle:@"Payment" atIndex:0 animated:YES];
    [inChargeType insertSegmentWithTitle:@"Charge" atIndex:1 animated:YES];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addButton:(id)sender {
    int result;
    
    if (inChargeType.selectedSegmentIndex == 0) {
        // payment
        result = 2;
    } else {
        // charge
        result = 1;
    }
    
    int patid = [[delegate ourPatron] patronId];
    int coid = 0;
    
    // generate the URL and use %20 encoding
    NSString *urlStr = [NSString stringWithFormat:@"http://www.cs.transy.edu/zoellers/inventory/charge.php?secret=pass&pid=%d&coid=%d&type=%d&amt=%@&notes=%@",patid,coid,result,inChargeAmt.text,inNotes.text];
    NSString *final = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"The url is: >>>>>%@<<<<<", final);
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:final]];
    
    // make web request and catch error
    NSError * e;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
    
    // log the value returned from the web server
    NSString *value = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"return from web: %@", value);
    
    
    [delegate refreshXML:nil];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (IBAction)cancelButton:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"addChargeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.detailTextLabel.text = @"";
    
    // make a text field
    UITextField *inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(85, 10, 530, 30)];
    inputTextField.adjustsFontSizeToFitWidth = YES;
    inputTextField.textColor = [UIColor blackColor];
    inputTextField.backgroundColor = [UIColor clearColor];
    inputTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
    inputTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
    inputTextField.tag = 0;
    inputTextField.clearButtonMode = UITextFieldViewModeNever;
    [inputTextField setEnabled: YES];
    inputTextField.delegate = self;
    
    // Configure the cell...
    switch ([indexPath row]) {
        case 0:
            cell.textLabel.text = @"Type";
            
            [cell.contentView addSubview:inChargeType];
            break;
        case 1:
            cell.textLabel.text = @"Amount";
            inputTextField.keyboardType = UIKeyboardTypeNumberPad;
            inputTextField.returnKeyType = UIReturnKeyNext;
            self.inChargeAmt = inputTextField;
            [cell.contentView addSubview:inputTextField];
            inputTextField = nil;
            break;
        case 2:
            cell.textLabel.text = @"Notes";
            inputTextField.keyboardType = UIKeyboardTypeDefault;
            inputTextField.returnKeyType = UIReturnKeyDone;
            self.inNotes = inputTextField;
            [cell.contentView addSubview:inputTextField];
            inputTextField = nil;
            break;
    }
    
    
    return cell;

}


@end
